﻿param(
	$solutionName = (throw 'Solution name required'),
	$solutionDirectory = (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)+'\',
	$outName = "$solutionName.Auto.ddf"
	)

#DDF Header -- BEGIN
$script:ddfFile = @"
.OPTION EXPLICIT 
.Set CabinetNameTemplate=$solutionName.WSP  
.Set Cabinet=on
.Set Compress=on
.Set DiskDirectoryTemplate=CDROM ; All cabinets go in single directory
.Set CompressionType=MSZIP ; All files compressed in cabinet files
.Set UniqueFiles="ON"
.Set DiskDirectory1=

"@
#DDF Header -- END

function recurse-directoryStructure($directoryPath)
{
	#Write-Host "Checking: " $directoryPath
	$f = ls $directoryPath -exclude *.wsp
	if($f -ne $null)
	{
		if($f.Length -gt 0)
		{
			$script:ddfFile += "
.Set DestinationDir=$($directoryPath.Substring($solutionDirectory.Length + $solutionName.Length).Trim('\'))
"
		}
		$f | sort PSIsContainer | %{
			if(!$_.PSIsContainer){
				add-fileToDDF($_.FullName)
			}
			else{
				recurse-directoryStructure($_.FullName)
			}
		}
	}
}

function add-fileToDDF([string]$fileLocation)
{
	$localFileLocation = $fileLocation.Substring($solutionDirectory.Length)
	$ddfLine = """$localFileLocation""
"
	Write-Host "Adding: " $localFileLocation
	$script:ddfFile += $ddfLine
}

write-host "Creating DDF"

recurse-directoryStructure($solutionDirectory+$solutionName)
$script:ddfFile | out-file -filePath ($solutionDirectory+$outName) -encoding UTF8 -force

